package com.kzw.oa.comm.service;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kzw.core.service.hibernate.DefaultEntityManager;
import com.kzw.core.util.ContextUtil;
import com.kzw.oa.comm.model.Log;
import com.kzw.oa.comm.type.LogType;
import com.kzw.oa.model.system.AppUser;

@Service
@Transactional
public class LogService extends DefaultEntityManager<Log, String> {
	
	@Autowired
	private  HttpServletRequest request;

	public void log(LogType type, HttpServletRequest request, String module) {
		AppUser user = ContextUtil.getCurrentUser();
		Log log = new Log();
		log.setLoginName(user.getUsername());
		log.setRealName(user.getFullname());
		// 一个用户多个部门 ？
		log.setDeptName(user.getDepartment().getDepName());
		log.setIp(request.getRemoteAddr());
		log.setHostname(request.getRemoteHost());
		log.setOpType(type.getName());
		String client = request.getHeader("User-Agent");
		log.setClient(client);
		log.setModule(module);
		saveOrUpdate(log);
	}
	
	public void log(String type, HttpServletRequest request, String module) {
		AppUser user = ContextUtil.getCurrentUser();
		Log log = new Log();
		log.setLoginName(user.getUsername());
		log.setRealName(user.getFullname());
		// 一个用户多个部门 ？
		log.setDeptName(user.getDepartment().getDepName());
		log.setIp(request.getRemoteAddr());
		log.setHostname(request.getRemoteHost());
		log.setOpType(type);
		String client = request.getHeader("User-Agent");
		log.setClient(client);
		log.setModule(module);
		saveOrUpdate(log);
	}
	
	public void login(AppUser user) {
		Log log = new Log();
		log.setLoginName(user.getUsername());
		log.setRealName(user.getFullname());
		log.setDeptName(user.getDepartment().getDepName());		
		log.setIp(request.getRemoteAddr());
		log.setHostname(request.getRemoteHost());
		log.setOpType("登陆");
		String client = request.getHeader("User-Agent");
		log.setClient(client);
		log.setModule("系统登陆");
		saveOrUpdate(log);
	}
}
